package tpc.data.tableRecord;

import java.text.ParseException;

import org.apache.hadoop.io.Text;

import unisb.cs.core.binary.utils.BinaryUtils;
import unisb.cs.data.tables.Record;
import unisb.cs.data.types.DataTypes;

public class PartRecord extends Record {
	
	protected static int size = 156;  
	protected static String tableName = "part";
	protected static int attributeNumber = 9;
	protected static int [] attributeType = {DataTypes.INT_TYPE, DataTypes.CHAR_TYPE, DataTypes.CHAR_TYPE, 
											 DataTypes.CHAR_TYPE, DataTypes.CHAR_TYPE, DataTypes.INT_TYPE, 
											 DataTypes.CHAR_TYPE, DataTypes.FLOAT_TYPE, DataTypes.CHAR_TYPE};
	protected static int [] attributeLength = {4, 60, 14,  8, 30,   4,  10,   4,  22}; 
	protected static int [] attributeOffset = {0,  4, 64, 78, 86, 116, 120, 130, 134}; 
	
	public void setRecord(String tuple) throws ParseException { 
		bytes = new byte[size];
		
		int pos = 0;
		int attributeIndex = 0;
		int len = tuple.length();
		StringBuffer sb = new StringBuffer();
		while(pos < len){
			if(tuple.charAt(pos) == '|'){
				if(attributeType[attributeIndex] == DataTypes.INT_TYPE){
					BinaryUtils.append(bytes, attributeOffset[attributeIndex], Integer.parseInt(sb.toString()));
				}
				else if(attributeType[attributeIndex] == DataTypes.FLOAT_TYPE){
					BinaryUtils.append(bytes, attributeOffset[attributeIndex], Float.parseFloat(sb.toString()));
				}
				else if(attributeType[attributeIndex] == DataTypes.DATE_TYPE){
					BinaryUtils.append(bytes, attributeOffset[attributeIndex], dateParser.parse(sb.toString()));
				}
				else{
					BinaryUtils.append(bytes, attributeOffset[attributeIndex], sb.toString().trim());
				}
				attributeIndex++;
				sb = new StringBuffer();
			}
			else
				sb.append(tuple.charAt(pos));
			
			pos++;				
		}
	}	

	public void setRecord(byte[] tuple) { 
		bytes = tuple;
	}

	public byte[] getBytes() {
		return bytes;
	}
	
	public String getStringRecord(){
		StringBuffer valueBuffer = new StringBuffer();
		
		for(int i = 0; i < attributeNumber; i++){			
			valueBuffer.append(getStringAttribute(i).trim() + "|");
		}
		return valueBuffer.toString();
	}
	
	public int getSize(){
		return size;
	}
	
	public byte[] getByteAttribute(int index) {
		return BinaryUtils.getBytes(bytes, attributeOffset[index], attributeLength[index]);
	}
	
	public String getStringAttribute(int index) {
	
		if(attributeType[index] == DataTypes.INT_TYPE)
			return Integer.toString(BinaryUtils.getInt(bytes, attributeOffset[index]));
		else if(attributeType[index] == DataTypes.FLOAT_TYPE)
			return Float.toString(BinaryUtils.getFloat(bytes, attributeOffset[index]));
		else if(attributeType[index] == DataTypes.DATE_TYPE)
			return BinaryUtils.getDate(bytes, attributeOffset[index], attributeLength[index]).toString();
		else
			return BinaryUtils.getString(bytes, attributeOffset[index], attributeLength[index]);
	}
	
	public String getTableName(){
		return tableName;
	}
	
	public int getAttributeNumber(){
		return attributeNumber;
	}
	
	public int getAttributeType(int index){
		return attributeType[index];
	}
	
	public int getAttributeLength(int index){
		return attributeLength[index];
	}
		
	public void getStringAttributes(Text value, int [] valueFieldIndex){
		
		StringBuffer valueBuffer = new StringBuffer();

		if(valueFieldIndex != null){
			for(int i = 0; i < valueFieldIndex.length; i++){
				valueBuffer.append(getStringAttribute(valueFieldIndex[i]).trim() + "|");
			}
		}
		else{
			for(int i = 0; i < attributeNumber; i++){
				valueBuffer.append(getStringAttribute(i).trim() + "|");
			}
		}
		value.set(valueBuffer.toString());
	}
	
	public String getStringAttributes(int [] valueFieldIndex){
		
		StringBuffer valueBuffer = new StringBuffer();

		if(valueFieldIndex != null){
			for(int i = 0; i < valueFieldIndex.length; i++){
				valueBuffer.append(getStringAttribute(valueFieldIndex[i]).trim() + "|");
			}
		}
		else{
			for(int i = 0; i < attributeNumber; i++){
				valueBuffer.append(getStringAttribute(i).trim() + "|");
			}
		}
		return valueBuffer.toString();
	}

	public void getByteAttributes(Text key, int keyFieldIndex){
		key.set(getByteAttribute(keyFieldIndex));
	}
	
	public String toString(){
		return getStringRecord();
	}
}
